
//  LoginMainVC.m
//  XJMTechnician
//  Created by Mr.hu on 2017/10/17.
//  Copyright © 2017年 Mr.hu. All rights reserved.

#import "LoginMainVC.h"
#import "ForgetSecretView.h"
#import "AccountRegister.h"
#import "IdentityReigster.h"

#import "RegisterIdentityCard.h"

@interface LoginMainVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *LoginHeadImage;
@property (weak, nonatomic) IBOutlet UITextField *InputPhoneNumber;
@property (weak, nonatomic) IBOutlet UITextField *InputSecret;

@property (weak, nonatomic) IBOutlet UIView *LoginFirstView;
@property (weak, nonatomic) IBOutlet UIView *LoginSecondView;
@property(strong,nonatomic)AFHTTPSessionManager * sessionmanage;
@property (weak, nonatomic) IBOutlet UIButton *LoginCommentSubmit;
@property (weak, nonatomic) IBOutlet UIButton *RegisterCommentSubmit;

@end

@implementation LoginMainVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //    [self.navigationController setNavigationBarHidden:YES animated:animated];
    
    self.LoginCommentSubmit.layer.cornerRadius = 22;
    self.RegisterCommentSubmit.layer.cornerRadius = 22;
    
    self.RegisterCommentSubmit.layer.borderWidth = 0.8;
    self.RegisterCommentSubmit.layer.borderColor = RGBA(234, 0, 0, 1).CGColor;
    
    self.InputPhoneNumber.delegate = self;
    self.InputSecret.delegate = self;
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jkr_tapGestureAction:)];
    tapGestureRecognizer.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapGestureRecognizer];
}

- (void)jkr_tapGestureAction:(UITapGestureRecognizer *)tapGusture{
    [self.view endEditing:YES];
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.InputPhoneNumber resignFirstResponder];
    [self.InputSecret resignFirstResponder];
    return NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    //    [self loginBaseController];
}

/*
 - (void)loginBaseController{
 
 self.LoginFirstView.layer.cornerRadius = 3;
 self.LoginFirstView.layer.masksToBounds = YES;
 self.LoginFirstView.layer.borderColor = [UIColor grayColor].CGColor;
 self.LoginFirstView.layer.borderWidth = 0.6;
 
 self.LoginSecondView.layer.cornerRadius = 3;
 self.LoginSecondView.layer.masksToBounds = YES;
 self.LoginSecondView.layer.borderColor = [UIColor grayColor].CGColor;
 self.LoginSecondView.layer.borderWidth = 0.6;
 
 self.InputPhoneNumber.delegate = self;
 self.InputSecret.delegate = self;
 
 }
 */

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [[self nextResponder] touchesBegan:touches withEvent:event];
    //super调用，别漏
    [super touchesBegan:touches withEvent:event];
    
    [self.InputPhoneNumber resignFirstResponder];
    [self.InputSecret resignFirstResponder];
}

#pragma mark-- 私有方法
- (IBAction)RegisterInformation:(id)sender {
    
    AccountRegister * forget = [[AccountRegister alloc]init];
    [self.navigationController pushViewController:forget animated:YES];
    
}

- (IBAction)ClearPhoneNumber:(id)sender {
    self.InputPhoneNumber.text = nil;
}

- (IBAction)SelectCheck:(UIButton*)sender {
    
    if (sender.selected == NO) {
        [sender setImage:[UIImage imageNamed:@"chick"] forState:UIControlStateNormal];
        sender.selected = YES;
        
    }else{
        
        [sender setImage:[UIImage imageNamed:@"unchick"] forState:UIControlStateNormal];
        sender.selected = NO;
    }
}

- (IBAction)UserProtocol:(id)sender {
    
}

- (IBAction)ImmediatelyLoginMeth:(id)sender {
    
    //    if ([XJToolClass checkTel:self.InputPhoneNumber.text] == YES) {
    if (self.InputPhoneNumber.text == nil || self.InputSecret.text == nil) {
        [XJToolClass tipAnimationWith:@"请输入手机号或者密码"];
    }else{
        [XJHttpClass LoginTechnicanPWd:self.InputSecret.text signature:self.InputPhoneNumber.text andFinishblock:^(id response, NSString *errStr) {
            
            if (errStr) {
                
                [XJToolClass tipAnimationWith:errStr];
                
            }else{
                
                NSDictionary * dictdata = response[@"data"];
                if ([dictdata isKindOfClass:[NSDictionary class]]) {
                    
                    if ([dictdata[@"status"] isEqualToString:@"0"]) {
                        
                        //补全信息 选择技能需要
                        [TechSimple registerpass].User_TechID = dictdata[@"tid"];
                        [TechSimple registerpass].User_TechPhone = self.InputPhoneNumber.text;
                        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"请补全个人身份信息!" preferredStyle:UIAlertControllerStyleAlert];
                        
                        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                            IdentityReigster * forget = [[IdentityReigster alloc]init];
                            [self.navigationController pushViewController:forget animated:YES];
                            
                        }];
                        
                        [alertController addAction:otherAction];
                        [self presentViewController:alertController animated:YES completion:nil];
                        
                    }else{
                        
                        [XJToolClass removeUserdic];
                        [XJToolClass saveUserDic:dictdata];
                        
                        
                        [XJToolClass saveUserId:dictdata[@"tid"]];
                        
                        [XJToolClass saveAccount:dictdata[@"mobile"]];
                        
                        [XJToolClass saveNickname:dictdata[@"nickname"]];
                        
                        [XJToolClass saveToken:dictdata[@"token"]];
                        
                        [XJToolClass savePasswd:dictdata[@"pwd"]];
                        
                        [XJToolClass saveHeadImg:dictdata[@"headurl"]];
                        
                        [XJToolClass SaveCurrentPhone:dictdata[@"mobile"]];
                        
                        NSString *headurl = dictdata[@"headurl"];
                        if (headurl) {
                            [XJToolClass saveAvater:headurl];
                        }
                        
                        [XJToolClass tipAnimationWith:@"登录成功"];
                        [self.navigationController popToRootViewControllerAnimated:YES];
                        
                    }
                }
            }
        }];
    }
}

- (IBAction)ForgetSecretMeth:(id)sender
{
    ForgetSecretView *forget = [[ForgetSecretView alloc] init];
    forget.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:forget animated:YES];
}

@end
